﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ProjectTask
{
    public partial class FrmTaskDeal : Form
    {
        ProjectCommon.ClassCommon cc;
        ClassTaskDeal ctdTaskDeal;
        ProjectCommon.ClassCommon.OperateState ccosState;

        public FrmTaskDeal(ProjectCommon.ClassCommon aClassCommon, ProjectCommon.ClassCommon.OperateState ccos, string JobTaskID)
        {
            InitializeComponent();
            cc = aClassCommon;
            ccosState = ccos;
            tbJobTaskID.Text = JobTaskID;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            tbFinishTime.Text = dtpFinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private bool TaskDealNewInitial()
        {
            bool result = false;
            this.Text += "---添加";
            tbFinishTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            tbEngineer.Text = cc.dtLoginUser.Rows[0]["RealName"].ToString();
            ctdTaskDeal = new ClassTaskDeal(cc.scKengee_IT_MIS);
            ctdTaskDeal.TaskDealNewInitial();
            return result;
        }

        private bool TaskDealModifyInitial()
        {
            bool result = false;
            this.Text += "---修改";
            ctdTaskDeal = new ClassTaskDeal(cc.scKengee_IT_MIS);
            result = ctdTaskDeal.GetTaskDeal_ID(tbJobTaskID.Text);
            ProjectCommon.ClassCommon.SetClass2Form(ctdTaskDeal.dtTaskDeal, this.Controls);
            return result;
        }

        private bool TaskViewInitial()
        {
            bool result = false;
            this.Text += "---浏览";
            ctdTaskDeal = new ClassTaskDeal(cc.scKengee_IT_MIS);
            result = ctdTaskDeal.GetTaskDeal_ID(tbJobTaskID.Text);
            ProjectCommon.ClassCommon.SetClass2Form(ctdTaskDeal.dtTaskDeal, this.Controls);
            btnSave.Visible = false;
            return result;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbEngineer.Text.Trim().Length < 1)
            {
                tbEngineer.Focus();
                MessageBox.Show("“执行人”不可为空");
            }
            else if (cbTaskState.Text.Trim().Length < 1)
            {
                cbTaskState.Focus();
                MessageBox.Show("“任务状态”不可为空");
            }
            else if (cbSolutionMode.Text.Trim().Length < 1)
            {
                cbSolutionMode.Focus();
                MessageBox.Show("“解决方式”不可为空");
            }
            else if (rtbSolution.Text.Trim().Length < 1)
            {
                rtbSolution.Focus();
                MessageBox.Show("“处理记录”不可为空");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                switch (ccosState)
                {
                    case ProjectCommon.ClassCommon.OperateState.osNew: TaskDealAppend(); break;
                    case ProjectCommon.ClassCommon.OperateState.osModify: TaskDealModify(); break;
                    case ProjectCommon.ClassCommon.OperateState.osView: { }; break;
                }
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
            this.Close();
        }

        private int TaskDealAppend()
        {
            int result = 0;

            if (cbTaskState.Text.Trim().Length<1)
            {
                MessageBox.Show("“任务状态”不可为空");
            }
            else if (cbSolutionMode.Text.Trim().Length < 1)
            {
                MessageBox.Show("“解决方式”不可为空");
            }
            else
            {
                tbFinishTime.Text = dtpFinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                ProjectCommon.ClassCommon.SetForm2Class(ctdTaskDeal.dtTaskDeal, this.Controls);
                result += ctdTaskDeal.TaskDealAppend();
            }

            return result;
        }

        private int TaskDealModify()
        {
            int result = 0;
            if (cbTaskState.Text.Trim().Length < 1)
            {
                MessageBox.Show("“任务状态”不可为空");
            }
            else if (cbSolutionMode.Text.Trim().Length < 1)
            {
                MessageBox.Show("“解决方式”不可为空");
            }
            else
            {
                tbFinishTime.Text = dtpFinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                ProjectCommon.ClassCommon.SetForm2Class(ctdTaskDeal.dtTaskDeal, this.Controls);
                result += ctdTaskDeal.TaskDealModify();
            }
            return result;
        }

        private void FrmTaskDeal_Shown(object sender, EventArgs e)
        {
            switch (ccosState)
            {
                case ProjectCommon.ClassCommon.OperateState.osNew: TaskDealNewInitial(); break;
                case ProjectCommon.ClassCommon.OperateState.osModify: TaskDealModifyInitial(); break;
                case ProjectCommon.ClassCommon.OperateState.osView: TaskViewInitial(); break;
            }
            ProjectCommon.ClassCommon.CopyComboBoxItem(ctdTaskDeal.cbTaskState, cbTaskState);
            ProjectCommon.ClassCommon.CopyComboBoxItem(ctdTaskDeal.cbSolutionMode, cbSolutionMode);
            ProjectCommon.ClassCommon.CopyComboBoxItem(ctdTaskDeal.cbSolutionMode, cbSolutionMode);
        }
    }
}
